package jpnms.src;

import org.junit.Test;

import java.lang.ref.SoftReference;
import java.math.BigInteger;
import java.util.*;

/**
 * @author jpnms
 * @version 1.0
 * @description: TODO
 * @date 2023/7/24 10:30
 */
public class CompanyTest {
    /**
     * 给定字符串数组，和整数k，返回出现次数前k的字符串和出现次数
     *
     * @param strings
     * @param k
     * @return
     */
    public String[][] TopString(String[] strings, int k) {
        HashMap<String, Integer> hashMap = new HashMap<>();
        for (String str : strings) {
            hashMap.put(str, hashMap.getOrDefault(str, 0) + 1);
        }
        ArrayList<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(hashMap.entrySet());
        list.sort(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue() - o1.getValue();
            }
        });
        String[][] results = new String[k > list.size() ? list.size() : k][2];

        for (int i = 0; i < list.size() && i < k; i++) {
            results[i][0] = list.get(i).getKey();
            results[i][1] = "" + list.get(i).getValue();
        }
        return results;
    }

    @Test
    public void test1() {
        String[] strings = new String[]{"sdf", "efs", "wegr", "sefvg", "efs", "wegr", "wegr", "sefvg", "efs",};
        String[][] strings1 = TopString(strings, 0);
        for (String[] s1 :
                strings1) {
            System.out.println(Arrays.toString(s1));
        }
    }

    /**
     * 俩非负整数相减
     *
     * @param min
     * @param sub
     * @return
     */
    public String subStrings(String min, String sub) {
        //从尾部开始，大值减小值，不足时借位
        int mid = 0;
        String s1 = min, s2 = sub;
        String signResult = "";
        if (min.equals(sub)) {
            return "";
        }
        if (min.length() == sub.length()) {
            for (int i = 0; i < min.length(); i++) {
                if (min.charAt(i) == sub.charAt(i)) continue;
                if (min.charAt(i) < sub.charAt(i)) {
                    s1 = sub;
                    s2 = min;
                    signResult = "-";
                    break;
                }
            }
        } else if (min.length() < sub.length()) {
            s1 = sub;
            s2 = min;
            signResult = "-";
        }
        StringBuilder sb = new StringBuilder(s1.length());
        for (int i = 0; i < s1.length(); i++) {
            char c1 = s1.charAt(s1.length() - 1 - i);
            char c2 = i < s2.length() ? s2.charAt(s2.length() - 1 - i) : '0';
            if (c1 - mid < c2) {
                sb.append(c1 + 10 - mid - c2);
                mid = 1;
            } else {
                sb.append(c1 - mid - c2);
                mid = 0;
            }
        }
        int zero = sb.length() - 1;
        while (sb.charAt(zero) == '0') zero--;
        return signResult + sb.reverse().toString().substring(sb.length()-1 - zero);    }

    /**
     * 俩非负整数相加
     *
     * @param min
     * @param sub
     * @return
     */
    public String addStrings(String min, String sub) {
        int mid = 0,size=Math.max(min.length(),sub.length());
        StringBuilder sb = new StringBuilder(min.length());

        //从尾部开始相加，记录进位
        for (int i = 0; i < size; i++) {
            char c1 = i < min.length() ? min.charAt(min.length() - 1 - i) : '0';
            char c2 = i < sub.length() ? sub.charAt(sub.length() - 1 - i) : '0';
            int tmp = c1-'0' + mid + c2-'0';
            if (tmp >= 10) {
                sb.append(tmp - 10);
                mid = 1;
            } else {
                sb.append(tmp);
                mid = 0;
            }
        }
        if(mid==1) return "1"+sb.reverse().toString();
        return sb.reverse().toString();
    }

    public String subStringsAdd(String min, String sub) {
        //先做符号处理
        String signMin = min.startsWith("-") ? "-" : "";
        String signSub = sub.startsWith("-") ? "-" : "";

        return null;
    }

    @Test
    public void testStringSub() {
        String s1 = "1941212";
        String s2 = "2341212";
        BigInteger b1 = new BigInteger(s1);
        BigInteger b2 = new BigInteger(s2);
        System.out.println(b1.subtract(b2));
        System.out.println(b1.add(b2).toString());
        System.out.println(subStrings(s1, s2));
    }

    @Test
    public void testStringAdd() {
        String s1 = "7941212";
        String s2 = "2341212";
        BigInteger b1 = new BigInteger(s1);
        BigInteger b2 = new BigInteger(s2);
        System.out.println(b1.add(b2));
        System.out.println(addStrings(s1, s2));
    }
}
